﻿using AutoMapper;
using MediatR;
using Production_Get.API.DTO;
using Production_Get.API.Infrastructures.Interfaces;
using Return.Enum;
using SlaughterItem.Common.Infrastructures.Interfaces;
using THE_WIND_RISES.Common.Domans.BasicInformation.Materials;
using THE_WIND_RISES.Common.Domans.Produce.Craft;
using THE_WIND_RISES.Common.Domans.Produce.ManifestInfos;
using THE_WIND_RISES.Common.Dtos;

namespace Production_Get.API.Application.Commands.Technologys
{
    public class ProcessRouteQueryHandler : IRequestHandler<ProcessRouteQueryCommand, THE_WIND_RISES.Common.Dtos.Result>
    {
        private readonly IBaseRepositoryGet<ProcessRouteDto> processRouterepository;
        public ProcessRouteQueryHandler(IBaseRepositoryGet<ProcessRouteDto> processRouterepository)
        {
            this.processRouterepository = processRouterepository;
          

        }

        public async Task<Result> Handle(ProcessRouteQueryCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCodes.Search };

            try
            {
                string sql = "select a.processRouteId,a.Handle,a.HandleTime,a.ProcessRouteNo,a.ProcessRouteName,a.processVersion,a.processDescription,a.inventoryInfoId,b.itemName,a.processRouteStatus from ProcessRoute a inner join ItemInfo b on a.InventoryInfoId=b.InventoryInfoId  where a.IsDelete=false";
                ProcessRouteDto com = new ProcessRouteDto();
                var list = processRouterepository.GetAll(sql, com).ToList();

                if (!string.IsNullOrEmpty(request.ProcessRouteNo))
                {
                    list = list.Where(x => x.ProcessRouteNo.Contains(request.ProcessRouteNo)).ToList();
                }

                if (!string.IsNullOrEmpty(request.ProcessRouteName))
                {
                    list = list.Where(x => x.ProcessRouteName.Contains(request.ProcessRouteName)).ToList();
                }

                if (request.InventoryInfoId > 0)
                {
                    list = list.Where(x => x.InventoryInfoId == request.InventoryInfoId).ToList();
                }

                //if (request.ProcessRouteStatus != null)
                //{
                //    list = list.Where(x => x.ProcessRouteStatus == request.ProcessRouteStatus).ToList();
                //}
                var totalCount = list.Count();

                var totalPage = (int)Math.Ceiling(totalCount * 1.0 / request.PageSize);

                list = list.OrderBy(x => x.ProcessRouteId).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();

                var aa = new
                {
                    list,
                    totalCount,
                    totalPage
                };
                if (list != null)
                {
                    result.Code = (int)CommonErrorCodes.Success;
                    result.Data = aa;
                    result.Msg = "显示成功";
                }
                else
                {
                    result.Msg = "显示失败";
                }
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }
    }
}
